import router from '@system.router';

var timerDuration = null;
var timerRhythm = null;
var timerPercent = null;
var rhythm = 0;
var times = 0;

export default {
    data: {
        // 旋转速度
        period: "",
        // 旋转几圈
        cycles: 0,
        // 节奏
        breath: "慢跑",
        // 百分比
        percent: 0,
        visible: true,
        // 还需坚持秒数
        duration: 0
    },
    onInit() {
        // 初始化还需要坚持的秒数
        if (this.durationSelected == 0) {
            this.duration = 6;
        } else if (this.durationSelected == 1) {
            this.duration = 120;
        } else if (this.durationSelected == 2) {
            this.duration = 180;
        }


        // 根据等级变换频率
        if (this.rhythmSelected == 0) {
            rhythm = 6;
        } else if (this.rhythmSelected == 1) {
            rhythm = 4;
        } else if (this.rhythmSelected == 2) {
            rhythm = 2;
        }


        // 计算变换次数
        times = this.duration / rhythm;

        // 旋转速度
        this.period = rhythm + "s";
        // 旋转几圈
        this.cycles = times;

    },
    onShow() {
        // 倒计时秒--
        timerDuration = setInterval(this.onDurationTimer, 1000);
        // 根据节奏变换快跑/慢跑
        timerRhythm = setInterval(this.onRhythmTimer, rhythm * 1000);
        // 百分比计算
        timerPercent = setInterval(this.onPercentTimer, rhythm / 100 * 1000);
    },
    // 页面销毁，清除定时器
    onDestroy() {
        clearInterval(timerDuration);
        timerDuration = null;
        clearInterval(timerRhythm);
        timerRhythm = null;
        clearInterval(timerPercent);
        timerPercent = null;
    },
    onSwipe(e) {
        if (e.direction == "left")
        if (this.visible == false) {
            router.push({
                uri: "pages/emotion/emotion"
            });
        }
    },
    onClick() {
        router.replace({
            uri: "pages/index/index"
        });
    },
    // 倒计时
    onDurationTimer() {
        this.duration--;

        if (this.duration <= 0) {
            clearInterval(timerDuration);
            timerDuration = null;

            this.visible = false;
        }
    },
    // 变换快跑/慢跑
    onRhythmTimer() {
        times--;

        if (times <= 0) {
            clearInterval(timerRhythm);
            timerRhythm = null;
            clearInterval(timerPercent);
            timerPercent = null;

            this.breath = "已完成";
            this.percent = 100;
        } else if (this.breath == "慢跑") {
            this.breath = "快跑";
            this.percent = 0;
        } else if (this.breath == "快跑") {
            this.breath = "慢跑";
            this.percent = 0;
        }
    },
    onPercentTimer() {
        this.percent++;
    }
}
